import { Component } from 'react'
import { View, Text } from '@tarojs/components'
import Taro from '@tarojs/taro'
import './index.scss'
import '../images/public/style.css'
// import { AtActivityIndicator } from 'taro-ui'
//公共组件
import LgNavCustomBar from '../../components/navCustomBar'
import LgSearch from '../../components/searchInput'
import LgButton from '../../components/button'
import LgSeleTarget from '../../components/seleTarget'
import LgCheckbox from '../../components/checkbox'
import LgEmptyContent from '../../components/emptyContent'
//个人组件
import PrSeleTarget from '../components/personal/seleTarget'
import PrPopChooseObject from '../components/personal/popChooseObject'

//工具
import { getStorage, getBrowserController } from '../../components/utils'
import md5 from '../utils/md5'
import { allIdentityCodeLib, idCodeTea, idCodeAdm } from '../utils/Lib'
import getInfo from '../utils/getInfo'
//api
import {
  API_getIdentityList,//获取身份列表（用于获取管理员类型）
  API_getUserIdentity,//获取指定身份用户列表
  API_getCollegeList,//获取学院列表（大学）
  API_getMajorList,//获取专业列表（大学）
  API_getGroupList,//获取教学组列表（区分大中小学）
  API_getUserTeachers,//获取教师列表
  API_getGradeList,//获取年级列表（区分大中小学）
  API_getClassList,//获取班级列表（区分大中小学）
  API_getUserStudents,//获取学生用户
  API_getUserParents,//获取家长用户
  API_getDepartmentList,//获取部门及列表
  API_getDepartmentStaffs,//获取部门员工列表
  API_getClassMine,//获取我的班级列表
  API_getGroupMine,//获取我的教师组列表
  API_getUserSearch,//搜索用户
  API_receiverCache,//刷新选择对象
} from '../../../network/servers'
import API from './request'

import { transmitUrlToVisit } from "../../../network/proxy"
import MyUtils from '../components/personal/utils'
import PrLoading from '../components/personal/loading'
import { getChecked2 } from './getChecked/schoolType2'
import { getNewData2 } from './getNewData/schoolType2'
import { getNewData1 } from './getNewData/schoolType1'


interface searchObj {
  name: string
  subData: []
  hasCho: 0 | 1 | 2//0-不选中，1-全选，2-半选
  type: number[]
  index: number
  Info: InfoObj
  search?: boolean//是否是搜索出来的值
}
interface InfoObj {
  UserName: string
  UserID: string
  PhotoPath: string
}

interface IndexState {
  // dataList:any[]
  choLibrary?: any[]//选择对象数据
  isPopOpen: boolean
  receivers?: receivers//接口返回的已选择对象
  receiver_pop: object//已选择的对象
  searchArr: searchObj[]
  searchChooseArr: searchObj[]//存储选择了的搜索成员
  searchOn: boolean//是否搜索了(是否展示搜索列表)
  searchLoading: boolean// 搜索加载中占位符
  noticeNature?: number// 1/校级；2/年级；3/班级；4/其他; 5/学校文件
  allCount?: number,
  isCollegeAdmin?:boolean
}

interface receivers {
  identities?: any[]
  teachers?: any[]
  students?: any[]
  parents?: any[]
  departments?: any[]
  grades?: any[]
  classes?: any[]
  persons?: any[]

}

if (process.env.TARO_ENV === 'weapp') {
  var app = Taro.getApp().$app
} else if (process.env.TARO_ENV === 'h5') {
  var browserController = getBrowserController()
}

const storage = getStorage()

export default class index extends Component<any, IndexState> {
  constructor(props) {
    super(props)
    this.state = {
      isPopOpen: false,
      receivers: {},
      receiver_pop: {},
      searchArr: [],
      searchChooseArr: [],
      searchOn: false,
      searchLoading: false,//搜索加载中占位符
      allCount: 0,
      isCollegeAdmin:false //是否为院系管理员
    }
    this.goBack = this.goBack.bind(this)

    this.getNewData = this.getNewData.bind(this)
    this.onChangeData = this.onChangeData.bind(this)

    this.deleteChoItem = this.deleteChoItem.bind(this)
    this.deleteChoItem2 = this.deleteChoItem2.bind(this)

    this.openPop = this.openPop.bind(this)
    this.closePop = this.closePop.bind(this)

    this.onSearch = this.onSearch.bind(this)
    this.onInput = this.onInput.bind(this)
    this.searchListOnClick = this.searchListOnClick.bind(this)
    this.closeSearchList = this.closeSearchList.bind(this)

    this.reflash = this.reflash.bind(this)

    this.setReceivers = this.setReceivers.bind(this)
  }

  componentWillMount() {
    // let ObjectData = MyUtils.getStorage("ObjectData")
    let ObjectData = []
    let searchChooseArr = MyUtils.getStorage("searchChooseArr")
    let toChooseObjectData = MyUtils.getStorage("toChooseObject")
    let receivers = toChooseObjectData.receivers
    this.setState({
      receivers: receivers,
      receiver_pop: {
        persons_pop: receivers.persons,
        allCount: receivers.persons?.length || 0
      }
    }, () => {
      console.log("路由1", toChooseObjectData, ObjectData, searchChooseArr)
      if (toChooseObjectData.noticeNature == undefined) {
        if (ObjectData != undefined && ObjectData?.length != 0) {
          console.log("找到了对象，直接使用");
          this.setState({
            choLibrary: ObjectData
          }, function () {
            this.onConfirm(2)
          })
        } else {
          console.log("未找到对象，重新请求")
          this.choLibraryInit()
        }
        if (ObjectData != undefined && searchChooseArr != []) {
          this.setState({
            searchChooseArr
          })
        }
      } else {
        MyUtils.setStorage("ObjectData", undefined)
        MyUtils.setStorage("searchChooseArr", undefined)
        this.setState({
          noticeNature: toChooseObjectData.noticeNature
        }, () => {
          this.choLibraryInit()
        })
      }
    })


  }

  componentDidMount() { }

  componentWillUnmount() { }

  componentDidShow() { }

  componentDidHide() { }
  //路由=============================
  goBack() {
    // let allData = this.getVal()
    let searchChooseArr = this.state.searchChooseArr
    console.log("返回", searchChooseArr)
    // MyUtils.setStorage("ObjectData",allData,false)
    // MyUtils.setStorage("searchChooseArr",searchChooseArr)
    Taro.navigateBack({
      delta: 1
    })
  }
  //网络请求===========================
  api_receiverCache() {//刷新选择对象  
    return new Promise<void>((resolve, reject) => {
      let headers = {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": getInfo.getToken()
      }
      API_receiverCache({}, headers).then(res => {
        console.log("api-刷新选择对象", res)
        resolve(res)
      }).catch(res => reject(res))
    })
  }

  api_search(value: string) {//搜索  value值
    if (!value) {
      return
    }
    Taro.showLoading({
      title: '搜索中'
    })
    let that = this
    let params = {
      appid: getInfo.getSysId(),
      access_token: this.getSecCode(getInfo.getSysId()),
      schoolId: getInfo.getSchoolId(),
      schoolID: getInfo.getSchoolId(),
      // userIDs: value,
      userName: value
    }
    let header = {
      "Content-Type": "application/x-www-form-urlencoded"
    }
    let url
    // let baseUrl = MyUtils.getStorage("schoolSystemInfo")?.baseUrl
    let baseUrl = getInfo.getBaseUrl()
    if (!baseUrl) {
      console.log("缺少基础平台地址")
      Taro.hideLoading()
      return
    }
    if (process.env.TARO_ENV === 'h5') {
      if (getInfo.getEnv() === "weapp") {//小程序嵌入H5
        url = baseUrl + '/BaseApi/UserMgr/UserInfoMgr/SearchUserByIDsOrName'
        url = transmitUrlToVisit(url)
      } else {//app嵌入H5
        url = baseUrl + '/BaseApi/UserMgr/UserInfoMgr/SearchUserByIDsOrName'
      }
    } else {//微信小程序
      let originUrl = baseUrl + '/BaseApi/UserMgr/UserInfoMgr/SearchUserByIDsOrName'
      url = transmitUrlToVisit(originUrl)
      console.log("url", originUrl, url)
      // url = baseUrl + '/BaseApi/UserMgr/UserInfoMgr/SearchUserByIDsOrName'
    }
    Taro.request({
      url: url,
      data: params,
      header: header,
      success: function (res) {
        console.log("api-搜索用户:", res, "\nparams:", params)
        Taro.hideLoading()
        let searchChooseArr = that.state.searchChooseArr
        if (res.statusCode == 200) {
          let searchArr: any[] = []
          res.data.Data.map((item, index) => {
            let hasCho = 0
            let index2 = searchChooseArr.findIndex(findItem => findItem.Info.UserID == item.UserID)
            // console.log(index2,searchChooseArr)
            if (index2 != -1) {
              hasCho = 1
            }
            searchArr.push({
              name: item.UserName,
              subData: [],
              hasCho,
              type: [index],
              search: true,
              index,
              Info: item
            })
          })
          searchArr = searchArr.slice(0, 200)//取前200个
          that.setState({
            searchOn: true,
            searchLoading: false,
            searchArr
          })
          console.log("搜索用户列表", searchArr)
        }
      }
    })
  }
  //搜索框===========================
  onSearch(data) {
    console.log("search", data)
    if (!data) {
      this.setState({
        searchOn: false
      })
      return
    }
    this.api_search(data)
  }

  debouncedFunc = MyUtils.debounce((fn) => {//防抖
    fn()
  }, 1000, false);

  onInput(data) {
    // console.log("input",data)
    if (!data) {
      return
    }
    if (!this.state.searchLoading || !this.state.searchOn) {
      this.setState({
        searchLoading: true,
        searchOn: true
      })
    }
    this.debouncedFunc(() => {
      this.api_search(data)
    })
  }

  closeSearchList() {//关闭搜索展示列表
    this.setState({
      searchArr: [],
      searchOn: false
    })
  }
  searchListOnClick(value, index, item) {//点击搜索出的列表的中的item
    let searchArr = this.state.searchArr
    let searchChooseArr = this.state.searchChooseArr
    console.log("点击一下", value, index, searchArr)
    searchArr[index].hasCho = value
    if (value == 1) {
      searchChooseArr.push(searchArr[index])
    } else if (value == 0) {
      let index1 = searchChooseArr.findIndex(searchItem => searchItem.Info.UserID == item.Info.UserID)
      searchChooseArr.splice(index1, 1)
      this.setState({
        searchChooseArr
      })
    }
    this.setState({
      searchArr
    })
    this.onChangeData(2)//更新数据
  }
  //刷新=============================
  reflash() {
    Taro.showLoading({
      title: '正在刷新',
    })
    this.api_receiverCache().then(res => {
      console.log("刷新成功,重新请求对象")
      Taro.hideLoading()
      this.setState({
        choLibrary: undefined,
      }, () => {
        this.choLibraryInit()
      })
    }).catch(res => {
      console.log("刷新接口调用失败,重新请求对象")
      Taro.hideLoading()
      this.setState({
        choLibrary: undefined,
      }, () => {
        this.choLibraryInit()
      })
    })
  }
  //返回webApp首页====================
  goMainPage() {
    if (process.env.TARO_ENV === 'h5') {
      if (browserController) {
        browserController.goHome()
      }
    } else {
      console.log("返回webApp首页函数,只能在webApp生效")
    }
  }
  //选择对象列表初始化=================
  IsTeaAdmin() {//判断是不是管理员身份的教师
    let identityCode = getInfo.getIdentityCode()
    let identityInfos = MyUtils.getStorage("userInfo").identityInfos

    if(getInfo.getSchoolId()==='S-XHWLXX'){
      return true
    }

    console.log("身份：", identityCode, "identityInfos", identityInfos);
    let idIndex = identityInfos.findIndex(item => item.identityCode == identityCode)
    if (idIndex == -1) {//身份列表中没有这个身份
      console.log("个人身份列表中不包含该身份:", identityCode)
      return false
    }

    let identityItem = identityInfos[idIndex]
    if (identityItem.userType != undefined) {
      if (identityItem.userType.split(",").findIndex(item => item == "0") != -1) {
        return true
      }
    } else {//兼容旧版没有身份的userType的情况
      if (allIdentityCodeLib[identityCode] != undefined) {//判断是否是自定义身份
        if (idCodeAdm.indexOf(identityCode) != -1) {
          console.log("有管理权限身份:", identityCode);
          return true
        }
      }
    }
    return false

  }

  //判断当前是否为院系管理员
  IsCollegeAdmin(){
    let identityCode = getInfo.getIdentityCode()
    let identityInfos = MyUtils.getStorage("userInfo").identityInfos

    let idIndex = identityInfos.findIndex(item => item.identityCode ==identityCode)
    if (idIndex == -1) {//身份列表中没有这个身份
      return false
    }
      return identityCode==="IC0009"
  }

  async choLibraryInit() {
    Taro.showLoading({
      title: '加载中' 
    })
    let noticeNature = this.state.noticeNature
    if (noticeNature != undefined && getInfo.getSchoolType() != 1) {
      if (noticeNature == 2) {//        // 1/校级；2/年级；3/班级；4/其他; 5/学校文件
        let choLibrary = await this.getGradeInit()
        choLibrary.forEach(element => {
          element.subData = []
        });
        Taro.hideLoading()
        this.setState({
          choLibrary: choLibrary
        }, () => {
          this.onConfirm(2)
        })
        return
      } else if (noticeNature == 3) {
        let choLibrary = await this.getGradeInit()
        Taro.hideLoading()
        this.setState({
          choLibrary: choLibrary
        }, () => {
          this.onConfirm(2)
        })
        return
      }
    }

    const noticeType=MyUtils.getStorage("toIssueNotice")?.noticeType  //1 紧急
    const isUrgeNotice=noticeType==1

    // console.log('isAdminTeacher', this.IsTeaAdmin())
    let choLibrary = [
      { name: "管理员", subData: undefined, hasCho: 0, type: [0] },
      { name: "教师", subData: undefined, hasCho: 0, type: [1] },
      { name: "学生", subData: undefined, hasCho: 0, type: [2] },
      { name: "家长", subData: undefined, hasCho: 0, type: [3] },
      { name: "学校组织结构", subData: undefined, hasCho: 0, type: [4] }
    ]
    let IsTeaAdmin = this.IsTeaAdmin()

    const isCollegeAdmin=this.IsCollegeAdmin()
    this.setState({isCollegeAdmin:isCollegeAdmin})

    // console.log("isCollegeAdmin",isCollegeAdmin,isUrgeNotice,noticeNature)

    if (getInfo.getUserType() == 1) {//教师端
      if (IsTeaAdmin) {
        choLibrary = [
          { name: "管理员", subData: undefined, hasCho: 0, type: [0] },
          { name: "教师", subData: undefined, hasCho: 0, type: [1] },
          { name: "学生", subData: undefined, hasCho: 0, type: [2] },
          { name: "家长", subData: undefined, hasCho: 0, type: [3] },
          { name: "学校组织结构", subData: undefined, hasCho: 0, type: [4] },
          { name: "我的班级", subData: undefined, hasCho: 0, type: [5] },
          { name: "学生家长", subData: undefined, hasCho: 0, type: [6] },
          { name: "我的教师组", subData: undefined, hasCho: 0, type: [7] },
        ]
      } else {
        choLibrary = [
          { name: "我的班级", subData: undefined, hasCho: 0, type: [5] },
          { name: "学生家长", subData: undefined, hasCho: 0, type: [6] },
        ]
      }
    } else {
      if(isCollegeAdmin){ //大学环境院系领导
        if(noticeNature==3){ //班级通知
          choLibrary = [
            { name: "我的班级", subData: undefined, hasCho: 0, type: [5] },
            { name: "学生家长", subData: undefined, hasCho: 0, type: [6] },
            { name: "我的教师组", subData: undefined, hasCho: 0, type: [7] },
          ]
        }else{
          choLibrary = [
            { name: "本系教师", subData: undefined, hasCho: 0, type: [8] },
            { name: "本系学生", subData: undefined, hasCho: 0, type: [9] },
          ]
        }
      }else{
        choLibrary = [
          { name: "管理员", subData: undefined, hasCho: 0, type: [0] },
          { name: "教师", subData: undefined, hasCho: 0, type: [1] },
          { name: "学生", subData: undefined, hasCho: 0, type: [2] },
          { name: "家长", subData: undefined, hasCho: 0, type: [3] },
          { name: "学校组织结构", subData: undefined, hasCho: 0, type: [4] }
        ]
      }
     
    }

    let PromiseArr: any[] = []

    if (getInfo.getUserType() == 1) {
      if (IsTeaAdmin) {
        PromiseArr.push(this.getAdminInit(choLibrary[0]))//获取管理员
        PromiseArr.push(this.getTeachersInit(choLibrary[1]))//获取教师
        PromiseArr.push(this.getStudentsInit(choLibrary[2]))//获取学生
        PromiseArr.push(this.getParentsInit(choLibrary[3]))//获取家长
        PromiseArr.push(this.getDepartmentInit(choLibrary[4]))//获取学校组织结构
        PromiseArr.push(this.getClassMineInit(choLibrary[5]))// 获取我的班级
        PromiseArr.push(this.getStuParentsInit(choLibrary[6]))// 获取学生家长
        PromiseArr.push(this.getGroupMineInit(choLibrary[7]))// 获取我的教研组
      } else {
        PromiseArr.push(this.getClassMineInit(choLibrary[0]))// 获取我的班级
        PromiseArr.push(this.getStuParentsInit(choLibrary[1]))// 获取学生家长
      }
    } else {
      if(isCollegeAdmin){
        if(noticeNature==3){
          PromiseArr.push(this.getClassMineInit(choLibrary[0]))// 获取我的班级
          PromiseArr.push(this.getStuParentsInit(choLibrary[1]))// 获取学生家长
          PromiseArr.push(this.getGroupMineInit(choLibrary[2]))// 获取我的教研组
        }else{
          PromiseArr.push(this.getMyCollegeTeacherInit(choLibrary[0]))//本系教师
          PromiseArr.push(this.getMyCollegeStudentInit(choLibrary[1])) //本系班级
        }
      }else{
        PromiseArr.push(this.getAdminInit(choLibrary[0]))//获取管理员
        PromiseArr.push(this.getTeachersInit(choLibrary[1]))//获取教师
        PromiseArr.push(this.getStudentsInit(choLibrary[2]))//获取学生
        PromiseArr.push(this.getParentsInit(choLibrary[3]))//获取家长
        PromiseArr.push(this.getDepartmentInit(choLibrary[4]))//获取学校组织结构
      }
    }

    Promise.all(PromiseArr).then((res) => {
      console.log("promiseRes",res)
      if (getInfo.getUserType() == 1) {
        if (IsTeaAdmin) {
          res.map(item => {
            if (item.index == 0) {//管理员
              choLibrary[0].subData = item.dataList
            } else if (item.index == 1) {//教师
              choLibrary[1].subData = item.dataList
            } else if (item.index == 2) {//学生
              choLibrary[2].subData = item.dataList
            } else if (item.index == 3) {//家长
              choLibrary[3].subData = item.dataList
            } else if (item.index == 4) {//学校组织结构
              choLibrary[4].subData = item.dataList
            } else if (item.index == 5) {//我的班级
              choLibrary[5].subData = item.dataList
            } else if (item.index == 6) {//学生家长
              choLibrary[6].subData = item.dataList
            } else if (item.index == 7) {//我的教学组
              choLibrary[7].subData = item.dataList
            }
          })
        } else {
          res.map(item => {
            if (item.index == 5) {//我的班级
              choLibrary[0].subData = item.dataList
            } else if (item.index == 6) {//学生家长
              choLibrary[1].subData = item.dataList
            }
          })
        }

      } else {
        if(isCollegeAdmin){
          if(noticeNature==3){
            res.map(item => {
             if (item.index == 0) {//我的班级
                choLibrary[0].subData = item.dataList
              } else if (item.index == 1) {//学生家长
                choLibrary[1].subData = item.dataList
              } else if (item.index ==2) {//我的教学组
                choLibrary[2].subData = item.dataList
              }
            })
          }else{
            res.map(item => {
              if (item.index == 0) {//本系教师
                 choLibrary[0].subData = item.dataList
               } else if (item.index == 1) {//本系班级
                 choLibrary[1].subData = item.dataList
               } 
             })
          }
        }else{
          res.map(item => {
            if (item.index == 0) {//管理员
              choLibrary[0].subData = item.dataList
            } else if (item.index == 1) {//教师
              choLibrary[1].subData = item.dataList
            } else if (item.index == 2) {//学生
              choLibrary[2].subData = item.dataList
            } else if (item.index == 3) {//家长
              choLibrary[3].subData = item.dataList
            } else if (item.index == 4) {//学校组织结构
              choLibrary[4].subData = item.dataList
            }
          })
        }
        
      }

      this.setState({
        choLibrary
      }, () => {
        this.onConfirm(2)
      })
      setTimeout(() => {
        Taro.hideLoading()
      }, 200);
    })
  }

  // handleTypeList(sublist, typeArr){//处理类型
  //   sublist.forEach((element,index) => {
  //     element.type = [...typeArr, index]
  //   });
  //   return sublist
  // }

  setReceivers(receivers) {//修改接口返回的已选中
    this.setState({
      receivers
    })
  }


  getAdminInit(data) {//管理员初始化
    let receivers = this.state.receivers
    return new Promise(resolve => {
      let schoolType = getInfo.getSchoolType()
      if (schoolType == 1) {//大学
        getNewData1.getIdentity(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "管理员", index: 0, dataList: res })
        })
      } else {//中小学
        getNewData2.getIdentity(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "管理员", index: 0, dataList: res })
        })
      }
    })
  }

  getTeachersInit(data) {//教师初始化
    let receivers = this.state.receivers
    return new Promise(resolve => {
      let schoolType = getInfo.getSchoolType()
      if (schoolType == 1) {//大学
        getNewData1.getTeachers(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "教师", index: 1, dataList: res })
        })
      } else {//中小学
        getNewData2.getTeachers(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "教师", index: 1, dataList: res })
        })
      }
    })
  }

  getStudentsInit(data) {//学生初始化
    let receivers = this.state.receivers
    return new Promise(resolve => {
      let schoolType = getInfo.getSchoolType()
      if (schoolType == 1) {//大学
        getNewData1.getStudents(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "学生", index: 2, dataList: res })
        })
      } else {//中小学
        getNewData2.getStudents(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "学生", index: 2, dataList: res })
        })
      }
    })
  }

  getParentsInit(data) {//家长初始化
    let receivers = this.state.receivers
    return new Promise(resolve => {
      let schoolType = getInfo.getSchoolType()
      if (schoolType == 1) {//大学
        getNewData1.getParents(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "家长", index: 3, dataList: res })
        })
      } else {//中小学
        getNewData2.getParents(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "家长", index: 3, dataList: res })
        })
      }
    })
  }

  getDepartmentInit(data) {//学校组织结构
    let receivers = this.state.receivers
    return new Promise(resolve => {
      let schoolType = getInfo.getSchoolType()
      // if (schoolType == 1) {//大学(和中小学一样的)
        getNewData1.getDepartment(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "学校组织结构", index: 4, dataList: res })
        })
      // } else {//中小学
      //   getNewData2.getDepartment(data, receivers, this.setReceivers).then(res => {
      //     resolve({ name: "学校组织结构", index: 4, dataList: res })
      //   })
      // }
    })
  }

  getClassMineInit(data) {//我的班级初始化
    let receivers = this.state.receivers
    return new Promise(resolve => {
      let schoolType = getInfo.getSchoolType()
      if (schoolType == 1) {//大学
        getNewData1.getMyClass(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "我的班级", index: 5, dataList: res })
        })
      } else {//中小学
        getNewData2.getMyClass(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "我的班级", index: 5, dataList: res })
        })
      }
    })
  }

  getStuParentsInit(data) {//学生家长初始化
    let receivers = this.state.receivers
    return new Promise(resolve => {
      let schoolType = getInfo.getSchoolType()
      if (schoolType == 1) {//大学
        getNewData1.getMyClassParents(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "学生家长", index: 6, dataList: res })
        })
      } else {//中小学
        getNewData2.getMyClassParents(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "学生家长", index: 6, dataList: res })
        })
      }
    })
  }

  getGroupMineInit(data) {//我的教师组初始化
    let receivers = this.state.receivers
    return new Promise(resolve => {
      let schoolType = getInfo.getSchoolType()
      if (schoolType == 1) {//大学
        getNewData1.getTeacherGroup(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "我的教研组", index: 7, dataList: res })
        })
      } else {//中小学
        getNewData2.getTeacherGroup(data, receivers, this.setReceivers).then(res => {
          resolve({ name: "我的教研组", index: 7, dataList: res })
        })
      }
    })
  }

  getGradeInit() {//获取年级初始化,返回数组
    let receivers = this.state.receivers
    return new Promise<any>((resolve, reject) => {
      let schoolType = getInfo.getSchoolType()
      if (schoolType == 1) {//大学

      } else {//中小学
        getNewData2.getGrade({}, receivers, this.setReceivers).then(res => {
          resolve(res)
        })
      }
      // API.api_getGradeList([]).then(res=>{
      //   resolve(res)
      // })
    })
  }

  getMyCollegeTeacherInit(data){
    let receivers = this.state.receivers
    return new Promise<any>((resolve, reject) => {
      let schoolType = getInfo.getSchoolType()
      if (schoolType == 1) {//大学
        getNewData1.getMyCollegeTeacher(data,receivers,this.setReceivers).then((res) => { 
          resolve({name:"本系教师",index:0,dataList:res})
         })
      } else {//中小学
      }
    })
  }

  getMyCollegeStudentInit(data){
    let receivers = this.state.receivers
    return new Promise<any>((resolve, reject) => {
      let schoolType = getInfo.getSchoolType()
      if (schoolType == 1) {//大学
        getNewData1.getMyCollegeStudent(data,receivers,this.setReceivers).then((res) => { 
          resolve({name:"本系学生",index:1,dataList:res})
         })
      } else {//中小学
      }
    })
  }

  //选择对象==========================
  setVal
  getVal
  onChangeData(allData) {
    this.onConfirm(2)
  }


  getNewData(data, level, fn) {
    console.log("data:",data,"\nlevel:",level)
    let schoolType = getInfo.getSchoolType()
    let noticeNature = this.state.noticeNature
    let receivers = this.state.receivers

    if (noticeNature != undefined && getInfo.getSchoolType() != 1) {
      if (noticeNature == 3) {//     1/校级；2/年级；3/班级；4/其他; 5/学校文件
        if (data.type.length == 1) {
          getNewData2.getClass(data, receivers, this.setReceivers).then(arr => {
            let datalist = [...arr]
            datalist.forEach(element => {
              element.subData = []
            });
            fn(datalist)
            setTimeout(() => {
              this.onConfirm(2)
            }, 1000);
          })
        }
        return
      }
    }
    if (schoolType == 1) {//大学
      if (data.type[0] == 0) {//管理员
        getNewData1.getIdentity(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 1) {//教师
        getNewData1.getTeachers(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 2) {//学生
        getNewData1.getStudents(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 3) {//家长
        getNewData1.getParents(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 4) {//学校组织结构
        getNewData1.getDepartment(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 5) {//我的班级
        getNewData1.getMyClass(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 6) {//学生家长（大学）
        getNewData1.getMyClassParents(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 7) {//我的教师组（大学）
        getNewData1.getTeacherGroup(data, receivers, this.setReceivers).then(arr => fn(arr))
      }else if (data.type[0] == 9) {//本系班级学生（大学）
        getNewData1.getMyCollegeStudent(data, receivers, this.setReceivers).then(arr => fn(arr))
      }
    } else {//中小学
      console.log(data)
      if (data.type[0] == 0) {//管理员
        getNewData2.getIdentity(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 1) {//教师
        getNewData2.getTeachers(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 2) {//学生
        getNewData2.getStudents(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 3) {//家长
        getNewData2.getParents(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 4) {//学校组织结构《1》
        getNewData1.getDepartment(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 5) {//我的班级
        getNewData2.getMyClass(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 6) {//学生家长(本班家长)
        getNewData2.getMyClassParents(data, receivers, this.setReceivers).then(arr => fn(arr))
      } else if (data.type[0] == 7) {//我的教师组
        getNewData2.getTeacherGroup(data, receivers, this.setReceivers).then(arr => fn(arr))
      }
    }
    setTimeout(() => {
      this.onConfirm(2)
    }, 1000);
  }

  async chooseMainObject(type) {//1-全体管理员，2-全体教师，3-全体学生，4-全体家长
    let allData = JSON.parse(JSON.stringify(this.getVal()))
    if (type == 1) {//全部管理员
      let isUnAllCheck = allData.findIndex(item => {//是否全选选中,-1:是, index:否
        if (item.type[0] == 0) {
          if (item.hasCho == 0 || item.hasCho == 2) {
            return true
          }
        }
      })
      for (let x = 0; x < allData.length; x++) {
        if (allData[x].type[0] == 0) {
          let value = 1
          if (isUnAllCheck == -1) { value = 0 }
          allData[x].hasCho = value
          allData[x].subData = this.handleChildren(allData[x].subData, value)
          await this.setVal(allData)
          this.onConfirm(2)
        }
      }
    } else if (type == 2) {//全体教师
      let isUnAllCheck = allData.findIndex(item => {//是否全选选中,-1:是, index:否
        if (item.type[0] == 1 || item.type[0] == 7) {
          if (item.hasCho == 0 || item.hasCho == 2) {
            return true
          }
        }
      })
      for (let x = 0; x < allData.length; x++) {
        if (allData[x].type[0] == 1 || allData[x].type[0] == 7) {
          let value = 1
          if (isUnAllCheck == -1) { value = 0 }
          allData[x].hasCho = value
          allData[x].subData = this.handleChildren(allData[x].subData, value)
          await this.setVal(allData)
          this.onConfirm(2)
        }
      }
    } else if (type == 3) {//全体学生
      let isUnAllCheck = allData.findIndex(item => {//是否全选选中,-1:是, index:否
        if (item.type[0] == 2 || item.type[0] == 5) {
          if (item.hasCho == 0 || item.hasCho == 2) {
            return true
          }
        }
      })
      for (let x = 0; x < allData.length; x++) {
        if (allData[x].type[0] == 2 || allData[x].type[0] == 5) {
          let value = 1
          if (isUnAllCheck == -1) { value = 0 }
          allData[x].hasCho = value
          allData[x].subData = this.handleChildren(allData[x].subData, value)
          await this.setVal(allData)
          this.onConfirm(2)
        }
      }
    } else if (type == 4) {//全体家长
      let isUnAllCheck = allData.findIndex(item => {//是否全选选中,-1:是, index:否
        if (item.type[0] == 3 || item.type[0] == 6) {
          if (item.hasCho == 0 || item.hasCho == 2) {
            return true
          }
        }
      })
      for (let x = 0; x < allData.length; x++) {
        if (allData[x].type[0] == 3 || allData[x].type[0] == 6) {
          let value = 1
          if (isUnAllCheck == -1) { value = 0 }
          allData[x].hasCho = value
          allData[x].subData = this.handleChildren(allData[x].subData, value)
          await this.setVal(allData)
          this.onConfirm(2)
        }
      }
    }
  }

  handleChildren(dataList, value) {
    if (!dataList || dataList.length == 0) {
      return dataList//没数据直接返回
    } else {
      for (let x = 0; x < dataList.length; x++) {
        dataList[x].hasCho = value
        if (dataList[x].subData != undefined && dataList[x].subData.length != 0) {
          this.handleChildren(dataList[x].subData, value)
        }
      }
    }
    return dataList
  }
  //点击确定=================================
  async onConfirm(type) {//type:1点击了确定并携带数据返回上一页,type:2更新已选择数据
    let allData = this.getVal()
    // console.log("allData",allData);
    let noticeNature = this.state.noticeNature// 1/校级；2/年级；3/班级；4/其他; 5/学校文件  
    let schoolType = getInfo.getSchoolType()// 1大学； 2中小学

    let receiver: any = {}
    let persons: any[] = []
    let receiver_pop: any = {}//弹窗使用的数据
    let persons_pop: any[] = []
    let showChoObjArr: string[] = []

    if (noticeNature == 2) {//年级
      let data = getChecked2.grades(allData)
      if (data.grades.length != 0) { receiver.grades = data.grades }
      if (data.grades_pop.length != 0) { receiver_pop.grades_pop = data.grades_pop }
      showChoObjArr.push(data.AllChecked ? "全部年级" : "部分年级")
    } else if (noticeNature == 3) {//班级
      let data = getChecked2.classes(allData)
      if (data.grades.length != 0) { receiver.grades = data.grades }
      if (data.grades_pop.length != 0) { receiver_pop.grades_pop = data.grades_pop }
      if (data.classes.length != 0) { receiver.classes = data.classes }
      if (data.classes_pop.length != 0) { receiver_pop.classes_pop = data.classes_pop }
      showChoObjArr.push(data.AllChecked ? "全部班级" : "部分班级")
    } else {
      allData.map((item, index) => {
        if (item.name == "管理员") {
          let identities: any[] = []
          let identities_pop: any[] = []
          if (item.hasCho == 0) {
            //无选中
          } else if (item.hasCho == 1) {//全选
            item.subData.map((item1, index) => {
              identities.push({ identityCode: item1.Info.identityCode, identityName: item1.Info.identityName })
              identities_pop.push(item1)
            })
            showChoObjArr.push("全部管理员")
          } else if (item.hasCho == 2) {//半选
            let a = this.handleAdmin(item, 1, { identities: [], persons: [], identities_pop: [], persons_pop: [] })
            identities = identities.concat(a.identities)
            persons = persons.concat(a.persons)

            identities_pop = identities_pop.concat(a.identities_pop)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("部分管理员")
          }
          if (identities.length != 0) { receiver.identities = identities }
          if (identities_pop.length != 0) { receiver_pop.identities_pop = identities_pop }
        } else if (item.name == "教师") {
          let teachers: any[] = []
          let teachers_pop: any[] = []
          let schoolType = getInfo.getSchoolType()
          if (item.hasCho == 0) {
            //无选中
          } else if (item.hasCho == 1) {
            if (schoolType == 1) {//大学
              item.subData.map((item1, index) => {
                teachers.push({ collegeId: item1.Info.collegeId, collegeName: item1.Info.collegeName })
                teachers_pop.push(item1)
              })
            } else {//中小学
              item.subData.map((item1, index) => {
                teachers.push({ subjectId: item1.Info.subjectId, subjectName: item1.Info.subjectName })
                teachers_pop.push(item1)
              })
            }
            showChoObjArr.push("全部教师")
          } else if (item.hasCho == 2) {
            let a = this.handleTeachers(item, 1, { teachers: [], persons: [], teachers_pop: [], persons_pop: [] }, schoolType)
            teachers = teachers.concat(a.teachers)
            persons = persons.concat(a.persons)
            teachers_pop = teachers_pop.concat(a.teachers_pop)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("部分教师")
          }
          receiver.teachers = [...(receiver.teachers || []), ...teachers]
          receiver_pop.teachers_pop = [...(receiver_pop.teachers_pop || []), ...teachers_pop]
        }else if(item.name==="本系教师"){
          let schoolType = getInfo.getSchoolType()
          if (item.hasCho == 0) {
            //无选中
          } else if (item.hasCho == 1) {
            let a = this.handleCollegeTeachers(item, 1, {  persons: [], persons_pop: [] }, schoolType)
            persons = persons.concat(a.persons)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("全部本系教师")
          } else if (item.hasCho == 2) {
            let a = this.handleCollegeTeachers(item, 1, {  persons: [], persons_pop: [] }, schoolType)
            persons = persons.concat(a.persons)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("部分教师")
          }
          receiver.persons = [...(receiver.persons || []), ...persons]
          receiver_pop.persons_pop = [...(receiver_pop.persons_pop || []), ...persons_pop]
        }else if(item.name==="本系学生"){
          let classes: any[] = []
          let classes_pop: any[] = []
          let schoolType = getInfo.getSchoolType()
          if (item.hasCho == 0) {
            //无选中
            let a = this.handleMyCollegeStudents(item, 1, { classes: [], persons: [], classes_pop: [], persons_pop: [] }, schoolType)
            classes = classes.concat(a.classes)
            persons = persons.concat(a.persons)
            // console.log("部分本系学生",a)
            console.log(this.state.receivers)
            classes_pop = classes_pop.concat(a.classes_pop)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("部分本系学生")
          } else if (item.hasCho == 1) {
            item.subData.map((item1, index) => {
              classes.push({ classId: item1.Info.classId, className: item1.Info.className })
              classes_pop.push(item1)
            })
            showChoObjArr.push("全部本系学生")
          } else if (item.hasCho == 2) {
            let a = this.handleMyCollegeStudents(item, 1, { classes: [], persons: [], classes_pop: [], persons_pop: [] }, schoolType)
            classes = classes.concat(a.classes)
            persons = persons.concat(a.persons)
            // console.log("部分本系学生",a)
            classes_pop = classes_pop.concat(a.classes_pop)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("部分本系学生")
          }
          console.log("本系学生",allData,item,classes,classes_pop)
          receiver.classes = [...(receiver.classes || []), ...classes]
          receiver_pop.classes_pop = [...(receiver_pop.classes_pop || []), ...classes_pop]
        } else if (item.name == "学生") {
          let students: any[] = []
          let students_pop: any[] = []
          let schoolType = getInfo.getSchoolType()
          if (item.hasCho == 0) {
            //无选中
          } else if (item.hasCho == 1) {
            if (schoolType == 1) {//大学
              item.subData.map((item1, index) => {
                students.push({ collegeId: item1.Info.collegeId, collegeName: item1.Info.collegeName })
                students_pop.push(item1)
              })
            } else {//中小学
              item.subData.map((item1, index) => {
                students.push({ gradeId: item1.Info.gradeId, gradeName: item1.Info.gradeName })
                students_pop.push(item1)
              })
            }
            showChoObjArr.push("全部学生")
          } else if (item.hasCho == 2) {
            let a = this.handleStudents(item, 1, { students: [], persons: [], students_pop: [], persons_pop: [] }, schoolType)
            students = students.concat(a.students)
            persons = persons.concat(a.persons)
            students_pop = students_pop.concat(a.students_pop)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("部分学生")
          }
          receiver.students = [...(receiver.students || []), ...students]
          receiver_pop.students_pop = [...(receiver_pop.students_pop || []), ...students_pop]
        } else if (item.name == "家长") {
          let parents: any[] = []
          let parents_pop: any[] = []
          let schoolType = getInfo.getSchoolType()
          if (item.hasCho == 0) {
            //无选中
          } else if (item.hasCho == 1) {
            if (schoolType == 1) {//大学
              item.subData.map((item1, index) => {
                parents.push({ collegeId: item1.Info.collegeId, collegeName: item1.Info.collegeName })
                parents_pop.push(item1)
              })
            } else {//中小学
              item.subData.map((item1, index) => {
                parents.push({ gradeId: item1.Info.gradeId, gradeName: item1.Info.gradeName })
                parents_pop.push(item1)
              })
            }
            showChoObjArr.push("全部家长")
          } else if (item.hasCho == 2) {
            let a = this.handleParents(item, 1, { parents: [], persons: [], parents_pop: [], persons_pop: [] }, schoolType)
            parents = parents.concat(a.parents)
            persons = persons.concat(a.persons)
            parents_pop = parents_pop.concat(a.parents_pop)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("部分家长")
          }
          receiver.parents = [...(receiver.parents || []), ...parents]
          receiver_pop.parents_pop = [...(receiver_pop.parents_pop || []), ...parents_pop]
        } else if (item.name == "学校组织结构") {
          let departments: any[] = []
          let departments_pop: any[] = []
          if (item.hasCho == 0) {
            //无选中
            let a = this.handleDepartments(item, 1, { departments: [], persons: [], departments_pop: [], persons_pop: [] })
            departments = departments.concat(a.departments)
            persons = persons.concat(a.persons)
            departments_pop = departments_pop.concat(a.departments_pop)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("部分部门成员")
          } else if (item.hasCho == 1) {
            item.subData.map((item1, index) => {
              departments.push({ departmentId: item1.Info.departmentId, departmentName: item1.Info.departmentName,nodeType:item1.nodeType })
              departments_pop.push(item1)
            })
            showChoObjArr.push("全部部门")
          } else if (item.hasCho == 2) {
            let a = this.handleDepartments(item, 1, { departments: [], persons: [], departments_pop: [], persons_pop: [] })
            departments = departments.concat(a.departments)
            persons = persons.concat(a.persons)
            departments_pop = departments_pop.concat(a.departments_pop)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("部分部门成员")
          }
          if (departments.length != 0) {
            receiver.departments = departments
          }
          if (departments_pop.length != 0) {
            receiver_pop.departments_pop = departments_pop
          }
        } else if (item.name == "我的班级") {
          let myClassStu: any[] = []
          let myClassStu_pop: any[] = []
          let schoolType = getInfo.getSchoolType()
          if (item.hasCho == 0) {
            //无选中
          } else if (item.hasCho == 1) {
            if (schoolType == 1) {//大学
              item.subData.map((item1, index) => {
                myClassStu.push({ classId: item1.Info.classId, className: item1.Info.className, classType: item1.Info.classType })
                myClassStu_pop.push(item1)
              })
            } else {//中小学
              item.subData.map((item1, index) => {
                myClassStu.push({ classId: item1.Info.classId, className: item1.Info.className, classType: item1.Info.classType })
                myClassStu_pop.push(item1)
              })
            }
            showChoObjArr.push("部分学生")
          } else if (item.hasCho == 2) {
            let a = this.handleMyClass(item, 1, { myClassStu: [], persons: [], myClassStu_pop: [], persons_pop: [] }, schoolType)
            myClassStu = myClassStu.concat(a.myClassStu)
            persons = persons.concat(a.persons)
            myClassStu_pop = myClassStu_pop.concat(a.myClassStu_pop)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("部分学生")
          }
          receiver.students = [...(receiver.students || []), ...myClassStu]
          receiver_pop.students_pop = [...(receiver_pop.students_pop || []), ...myClassStu_pop]
        } else if (item.name == "学生家长") {
          let StuParents: any[] = []
          let StuParents_pop: any[] = []
          let schoolType = getInfo.getSchoolType()
          if (item.hasCho == 0) {
            //无选中
          } else if (item.hasCho == 1) {
            if (schoolType == 1) {//大学
              console.log("学生家长大学", item)
              item.subData.map((item1, index) => {
                StuParents.push({ classId: item1.Info.classId, className: item1.Info.className, classType: item1.Info.classType })
                StuParents_pop.push(item1)
              })
            } else {//中小学
              item.subData.map((item1, index) => {
                StuParents.push({ classId: item1.Info.classId, className: item1.Info.className, classType: item1.Info.classType })
                StuParents_pop.push(item1)
              })
            }
            showChoObjArr.push("部分家长")
          } else if (item.hasCho == 2) {
            let a = this.handleStuParents(item, 1, { StuParents: [], persons: [], StuParents_pop: [], persons_pop: [] }, schoolType)
            StuParents = StuParents.concat(a.StuParents)
            persons = persons.concat(a.persons)
            StuParents_pop = StuParents_pop.concat(a.StuParents_pop)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("部分家长")
          }
          receiver.parents = [...(receiver.parents || []), ...StuParents]
          receiver_pop.parents_pop = [...(receiver_pop.parents_pop || []), ...StuParents_pop]
        } else if (item.name == "我的教师组" || item.name == "我的教研组") {
          let MyGroup: any[] = []
          let MyGroup_pop: any[] = []
          let schoolType = getInfo.getSchoolType()
          if (item.hasCho == 0) {
            //无选中
          } else if (item.hasCho == 1) {
            if (schoolType == 1) {//大学
              item.subData.map((item1, index) => {
                MyGroup.push({ groupId: item1.Info.groupId, groupName: item1.Info.groupName })
                MyGroup_pop.push(item1)
              })
            } else {//中小学
              item.subData.map((item1, index) => {
                MyGroup.push({ subjectId: item1.Info.subjectId, subjectName: item1.Info.subjectName })
                MyGroup_pop.push(item1)
              })
            }
            showChoObjArr.push("部分教师")
          } else if (item.hasCho == 2) {
            let a = this.handleMyGroup(item, 1, { MyGroup: [], persons: [], MyGroup_pop: [], persons_pop: [] }, schoolType)
            MyGroup = MyGroup.concat(a.MyGroup)
            persons = persons.concat(a.persons)
            MyGroup_pop = MyGroup_pop.concat(a.myClassStu_pop)
            persons_pop = persons_pop.concat(a.persons_pop)
            showChoObjArr.push("部分教师")
          }
          receiver.teachers = [...(receiver.teachers || []), ...MyGroup]
          receiver_pop.teachers_pop = [...(receiver_pop.teachers_pop || []), ...MyGroup_pop]
        }
      })
    }

    //搜索
    // let searchArr = this.state.searchArr
    let searchChooseArr = this.state.searchChooseArr
    if (searchChooseArr.length > 0) {
      let search: any[] = []
      let search_pop: any[] = []
      searchChooseArr.map(item => {
        if (item.hasCho == 1) {
          search.push({
            userName: item.Info.UserName,
            userId: item.Info.UserID,
            photoPath: item.Info.PhotoPath
          })
          search_pop.push(item)
        }
      })
      persons = persons.concat(search)
      persons_pop = persons_pop.concat(search_pop)
      if (search) {
        showChoObjArr.push("个人")
      }
    }

    if (persons.length != 0) {
      receiver.persons = persons
    }
    if (persons_pop.length != 0) {
      receiver_pop.persons_pop = persons_pop
    }

    // console.log("receiver/",receiver)
    // console.log("showChoObjArr",showChoObjArr);
    // console.log("receiver_pop",receiver_pop)

    if (type == 1) {//点击了确定并携带数据返回上一页
      let originReceiver = this.state.receivers
      if (!receiver.departments?.length && originReceiver?.departments && originReceiver.departments.length != 0) {
        showChoObjArr.push("部分部门成员")
      }
      if (!receiver.identities?.length && originReceiver?.identities && originReceiver.identities.length != 0) {
        showChoObjArr.push("部分管理员")
      }
      if (!receiver.teachers?.length && originReceiver?.teachers && originReceiver.teachers.length != 0) {
        showChoObjArr.push("部分教师")
      }
      if (!receiver.students?.length && originReceiver?.students && originReceiver.students.length != 0) {
        showChoObjArr.push("部分学生")
      }
      if (!receiver.parents?.length && originReceiver?.parents && originReceiver.parents.length != 0) {
        showChoObjArr.push("部分家长")
      }
      if (!receiver.persons?.length && originReceiver?.persons && originReceiver.persons.length != 0) {
        showChoObjArr.push("个人")
      }
      if (!receiver.grades?.length && originReceiver?.grades && originReceiver.grades.length != 0) {
        showChoObjArr.push("年级")
      }
      if (!receiver.persons?.length && originReceiver?.persons && originReceiver.persons.length != 0) {
        showChoObjArr.push("班级")
      }

      receiver.departments = [...(receiver.departments || []), ...(originReceiver?.departments || [])]
      receiver.identities = [...(receiver.identities || []), ...(originReceiver?.identities || [])]
      receiver.teachers = [...(receiver.teachers || []), ...(originReceiver?.teachers || [])]
      receiver.students = [...(receiver.students || []), ...(originReceiver?.students || [])]
      receiver.parents = [...(receiver.parents || []), ...(originReceiver?.parents || [])]
      receiver.persons = [...(receiver.persons || []), ...(originReceiver?.persons || [])]
      receiver.grades = [...(receiver.grades || []), ...(originReceiver?.grades || [])]
      receiver.classes = [...(receiver.classes || []), ...(originReceiver?.classes || [])]

      if (receiver.departments.length == 0) {//如果没有就清除对象
        delete receiver.departments
      }
      if (receiver.identities.length == 0) {
        delete receiver.identities
      }
      if (receiver.teachers.length == 0) {
        delete receiver.teachers
      }
      if (receiver.students.length == 0) {
        delete receiver.students
      }
      if (receiver.parents.length == 0) {
        delete receiver.parents
      }
      if (receiver.persons.length == 0) {
        delete receiver.persons
      }
      if (receiver.grades.length == 0) {
        delete receiver.grades
      }
      if (receiver.classes.length == 0) {
        delete receiver.classes
      }

      if (
        receiver.departments?.length ||
        receiver.identities?.length ||
        receiver.parents?.length ||
        receiver.students?.length ||
        receiver.teachers?.length ||
        receiver.persons?.length ||
        receiver.grades?.length ||
        receiver.classes?.length
      ) {
        MyUtils.setStorage("chooseObject", receiver)
        MyUtils.setStorage("showChoObjArr", showChoObjArr)
        console.log("ObjectData&searchChooseArr", receiver, allData, searchChooseArr)
        // MyUtils.setStorage("ObjectData",allData,false)
        MyUtils.setStorage("searchChooseArr", searchChooseArr)

        // console.log("receiver",receiver)
        // console.log("showChoObjArr",showChoObjArr)
        Taro.navigateBack({
          delta: 1
        })
      } else {
        Taro.showToast({
          title: "请选择对象",
          icon: 'none',
          duration: 2000
        })
      }
    } else if (type == 2) {//更新已选择数据《2》
      console.log("统计已选择人数", receiver_pop)
      // let receivers = this.state.receivers
      // console.log("receivers",receivers)
      this.setState({
        receiver_pop
      })
    }
  }

  //管理员处理
  handleAdmin(data, level, objStore) {
    data.subData.map(item => {
      // console.log(item,'管理员处理')
      if (item.hasCho == 0) {
        //无选中
      } else if (item.hasCho == 1) {
        if (level == 1) {
          objStore.identities.push({ identityCode: item.Info.identityCode, identityName: item.Info.identityName })
          objStore.identities_pop.push(item)
        } else if (level == 2) {
          objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
          objStore.persons_pop.push(item)
        }
      } else if (item.hasCho == 2) {
        this.handleAdmin(item, level + 1, objStore)
      }
    })
    return objStore
  }
  //教师处理
  handleTeachers(data, level, objStore, schoolType) {
    if (schoolType == 1) {//大学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.teachers.push({ collegeId: item.Info.collegeId, collegeName: item.Info.collegeName })
            objStore.teachers_pop.push(item)
          } else if (level == 2) {
            objStore.teachers.push({ groupId: item.Info.groupId, groupName: item.Info.groupName })
            objStore.teachers_pop.push(item)
          } else if (level == 3) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleTeachers(item, level + 1, objStore, schoolType)
        }
      })
    } else {//中小学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.teachers.push({ subjectId: item.Info.subjectId, subjectName: item.Info.subjectName })
            objStore.teachers_pop.push(item)
          } else if (level == 2) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleTeachers(item, level + 1, objStore, schoolType)
        }
      })
    }
    return objStore
  }
  
  //院系教师处理
  handleCollegeTeachers(data, level, objStore, schoolType) {
    if (schoolType == 1) {//大学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleCollegeTeachers(item, level + 1, objStore, schoolType)
        }
      })
    }
    return objStore
  }

  //学生处理
  handleStudents(data, level, objStore, schoolType) {
    if (schoolType == 1) {//大学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.students.push({ collegeId: item.Info.collegeId, collegeName: item.Info.collegeName })
            objStore.students_pop.push(item)
          } else if (level == 2) {
            objStore.students.push({ majorId: item.Info.majorId, majorName: item.Info.majorName })
            objStore.students_pop.push(item)
          } else if (level == 3) {
            objStore.students.push({ gradeId: item.Info.gradeId, gradeName: item.Info.gradeName })
            objStore.students_pop.push(item)
          } else if (level == 4) {
            objStore.students.push({ classId: item.Info.classId, className: item.Info.className })
            objStore.students_pop.push(item)
          } else if (level == 5) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleStudents(item, level + 1, objStore, schoolType)
        }
      })
    } else {//中小学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.students.push({ gradeId: item.Info.gradeId, gradeName: item.Info.gradeName })
            objStore.students_pop.push(item)
          } else if (level == 2) {
            objStore.students.push({ classId: item.Info.classId, className: item.Info.className })
            objStore.students_pop.push(item)
          } else if (level == 3) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleStudents(item, level + 1, objStore, schoolType)
        }
      })
    }
    return objStore
  }

  //院系学生处理
  handleMyCollegeStudents(data, level, objStore, schoolType) {
    console.log("handleMyCollegeStudents",data, level, objStore, schoolType)
    if (schoolType == 1) {//大学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.classes.push({ classId: item.Info.classId, className: item.Info.className })
            objStore.classes_pop.push(item)
          }else if (level == 2) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleMyCollegeStudents(item, level + 1, objStore, schoolType)
        }
      })
    } else {//中小学
    }
    return objStore
  }

  //家长处理
  handleParents(data, level, objStore, schoolType) {
    if (schoolType == 1) {//大学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.parents.push({ collegeId: item.Info.collegeId, collegeName: item.Info.collegeName })
            objStore.parents_pop.push(item)
          } else if (level == 2) {
            objStore.parents.push({ majorId: item.Info.majorId, majorName: item.Info.majorName })
            objStore.parents_pop.push(item)
          } else if (level == 3) {
            objStore.parents.push({ gradeId: item.Info.gradeId, gradeName: item.Info.gradeName })
            objStore.parents_pop.push(item)
          } else if (level == 4) {
            objStore.parents.push({ classId: item.Info.classId, className: item.Info.className })
            objStore.parents_pop.push(item)
          } else if (level == 5) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleParents(item, level + 1, objStore, schoolType)
        }
      })
    } else {//中小学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.parents.push({ gradeId: item.Info.gradeId, gradeName: item.Info.gradeName })
            objStore.parents_pop.push(item)
          } else if (level == 2) {
            objStore.parents.push({ classId: item.Info.classId, className: item.Info.className })
            objStore.parents_pop.push(item)
          } else if (level == 3) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleParents(item, level + 1, objStore, schoolType)
        }
      })
    }
    return objStore
  }
  //部门处理
  handleDepartments(data, level, objStore) {
    data.subData.map(item => {
      if (item.hasCho == 0) {
        //无选中
      } else if (item.hasCho == 1) {
        if (item.Info.userId != undefined) {
          console.log("个人")
          objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
          objStore.persons_pop.push(item)
        } else if (item.Info.departmentId != undefined) {
          console.log("部门")
          objStore.departments.push({ departmentId: item.Info.departmentId, departmentName: item.Info.departmentName })
          objStore.departments_pop.push(item)
        }
      } else if (item.hasCho == 2) {
        this.handleDepartments(item, level + 1, objStore)
      }
    })
    return objStore
  }

  //我的班级处理
  handleMyClass(data, level, objStore, schoolType) {
    if (schoolType == 1) {//大学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.myClassStu.push({ classId: item.Info.classId, className: item.Info.className, classType: item.Info.classType })
            objStore.myClassStu_pop.push(item)
          } else if (level == 2) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleMyClass(item, level + 1, objStore, schoolType)
        }
      })
    } else {//中小学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {//??
            objStore.myClassStu.push({ classId: item.Info.classId, className: item.Info.className, classType: item.Info.classType })
            objStore.myClassStu_pop.push(item)
          } else if (level == 2) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleMyClass(item, level + 1, objStore, schoolType)
        }
      })
    }
    return objStore
  }

  //学生家长处理
  handleStuParents(data, level, objStore, schoolType) {
    if (schoolType == 1) {//大学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.StuParents.push({ classId: item.Info.classId, className: item.Info.className, classType: item.Info.classType })
            objStore.StuParents_pop.push(item)
          } else if (level == 2) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleStuParents(item, level + 1, objStore, schoolType)
        }
      })
    } else {//中小学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.StuParents.push({ classId: item.Info.classId, className: item.Info.className, classType: item.Info.classType })
            objStore.StuParents_pop.push(item)
          } else if (level == 2) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleStuParents(item, level + 1, objStore, schoolType)
        }
      })
    }
    return objStore
  }

  //我的教师组处理
  handleMyGroup(data, level, objStore, schoolType) {
    if (schoolType == 1) {//大学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.MyGroup.push({ groupId: item.Info.groupId, groupName: item.Info.groupName })
            objStore.MyGroup_pop.push(item)
          } else if (level == 2) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleMyGroup(item, level + 1, objStore, schoolType)
        }
      })
    } else {//中小学
      data.subData.map(item => {
        if (item.hasCho == 0) {
          //无选中
        } else if (item.hasCho == 1) {
          if (level == 1) {
            objStore.MyGroup.push({ subjectId: item.Info.subjectId, subjectName: item.Info.subjectName })
            objStore.MyGroup_pop.push(item)
          } else if (level == 2) {
            objStore.persons.push({ userId: item.Info.userId, userName: item.Info.userName, photoPath: item.Info.photoPath })
            objStore.persons_pop.push(item)
          }
        } else if (item.hasCho == 2) {
          this.handleMyGroup(item, level + 1, objStore, schoolType)
        }
      })
    }
    return objStore
  }

  choPersonCount() {
    let allData = this.getVal()
    let admin: any = []
    allData.map(item1 => {
      if (item1.name == "管理员") {
        if (item1.hasCho == 0) {

        } else if (item1.hasCho == 1) {
          item1.subData.map(item2 => {
            admin.push(item2)
          })
        } else if (item1.hasCho == 2) {
          item1.subData.map(item2 => {
            if (item2.hasCho == 0) {

            } else if (item2.hasCho == 1) {
              admin.push(item2)
            } else if (item2.hasCho == 2) {

            }
          })
        }
      }
    })
  }

  //选择对象弹窗====================================
  deleteChoItem(item) {//移除对象
    let allData = JSON.parse(JSON.stringify(this.getVal()))
    console.log("allData", allData, "\n移除所选", item)
    if (item.search) {//对象是搜索得出的内容
      let searchArr = JSON.parse(JSON.stringify(this.state.searchArr))
      let searchChooseArr = JSON.parse(JSON.stringify(this.state.searchChooseArr))
      // searchArr[item.index].hasCho = 0
      let index1 = searchChooseArr.findIndex(searchItem => searchItem.Info.UserID == item.Info.UserID)
      if (index1 != -1) {
        searchChooseArr.splice(index1, 1)
      }
      let index2 = searchArr.findIndex(searchItem => searchItem.Info.UserID == item.Info.UserID)
      if (index2 != -1) {
        searchArr[index2].hasCho = 0
      }
      this.setState({
        searchChooseArr,
        searchArr
      }, function () {
        this.onConfirm(2)
      })
    } else {
      let noticeNature = this.state.noticeNature
      if (noticeNature != undefined && getInfo.getSchoolType() != 1) {
        if (noticeNature == 2) {//年级
          let typeArr = item.type
          // console.log("typeArr",typeArr)
          // console.log("allData",allData)
          allData = this.handleCho2(allData, typeArr, 0, 0)
          this.setVal(allData)
          this.onConfirm(2)
          return
        } else if (noticeNature == 3) {//班级
          let typeArr = item.type
          // console.log("typeArr",typeArr)
          // console.log("allData",allData)
          allData = this.handleCho2(allData, typeArr, 0, 0)
          this.setVal(allData)
          this.onConfirm(2)
          return
        }
      }
      let typeArr = item.type//[1,2,3]
      allData = this.handleCho(allData, typeArr, 0, 0)
      this.setVal(allData)
      setTimeout(() => {
        this.onConfirm(2)
      }, 1000);

    }
  }
  deleteChoItem2(item, type, index) {//移除所选(接口返回的对象)
    console.log("移除所选(接口返回的对象)", item)
    let receivers = this.state.receivers
    receivers?.[type]?.splice(index, 1)
    this.setState({
      receivers
    })
  }

  handleCho2(dataList, typeArr, level, value) {
    let length = typeArr.length
    console.log("datalist222",dataList,typeArr,level,value)
    if (level == length - 1) {
      dataList[typeArr[level]].hasCho = value
      this.handleChildren(dataList[typeArr[level]].subData, 0)
    } else {
      if (level == length - 2) {
        let state = 0
        dataList[typeArr[level]].subData.map((item, index) => {
          if (item.hasCho != 0 && index != typeArr[level + 1]) {
            state = 2
          }
        })
        dataList[typeArr[level]].hasCho = state
      }
      this.handleCho2(dataList[typeArr[level]].subData, typeArr, level + 1, value)
    }
    return dataList
  }

  handleCho(dataList, typeArr, level, value) {
    let length = typeArr.length
    console.log("datalist",dataList,typeArr,level,value)

    if (level == 0) {
      for (let x = 0; x < dataList.length; x++) {
        if (dataList[x].type[0] == typeArr[0]) {
          console.log("dataList[x]", dataList[x])

          let state = 0
          dataList[x].subData.map((item, index) => {
            console.log(item)
            if (item.hasCho != 0 && index != typeArr[level + 1]) { //当子项选中或者半选中 且 当先所在不是是删除子项的路径上 
              state = 2
            }
          })
          dataList[x].hasCho = state
          this.handleCho(dataList[x].subData, typeArr, level + 1, value)
        }
      }
    } else if (level == length - 1) {
      dataList[typeArr[level]].hasCho = value
      this.handleChildren(dataList[typeArr[level]].subData, 0)
    } else {
      if (dataList[typeArr[level]].hasCho == 1) {
        dataList[typeArr[level]].hasCho = 2
      }
      if (level == length - 2) {
        let state = 0
        dataList[typeArr[level]].subData.map((item, index) => {
          if (item.hasCho != 0 && index != typeArr[level + 1]) {
            state = 2
          }
        })
        dataList[typeArr[level]].hasCho = state
      }
      this.handleCho(dataList[typeArr[level]].subData, typeArr, level + 1, value)
    }
    return dataList
  }

  openPop() {
    this.choPersonCount()
    this.setState({
      isPopOpen: true
    })
  }
  closePop() {
    this.setState({
      isPopOpen: false
    })
  }
  //taro弹窗================================
  showToast(title, icon, duration) {
    Taro.hideLoading()
    setTimeout(() => {
      Taro.showToast({
        title: title,
        icon: icon,
        duration: duration
      })
    }, 500);
  }
  //参数加密==========================
  getSecCode(string) {//md5加密反序
    let b = md5.hex_md5_32(string).split('').reverse().join('')
    return b
  }
  //render中数据处理====================
  getAllpersonCount(data) {//计算选择的人数
    console.log('计算人数', data)
    let allpersonCount = 0
    if (data?.identities_pop?.length) {
      data.identities_pop.map(item => {
        if (item.Info.count) {
          allpersonCount = allpersonCount + item.Info.count * 1
        }
      })
    }
    if (data?.teachers_pop?.length) {
      data.teachers_pop.map(item => {
        if (item.Info.teacherCount) {
          allpersonCount = allpersonCount + item.Info.teacherCount * 1
        } else if (item.Info.count) {
          allpersonCount = allpersonCount + item.Info.count * 1
        }
      })
    }
    if (data?.students_pop?.length) {
      data.students_pop.map(item => {
        if (item.Info.studentCount) {
          allpersonCount = allpersonCount + item.Info.studentCount * 1
        } else if (item.Info.count) {
          allpersonCount = allpersonCount + item.Info.count * 1
        }
      })
    }
    if (data?.parents_pop?.length) {
      data.parents_pop.map(item => {
        if (item.Info.studentCount) {//家长数量就是学生数量
          allpersonCount = allpersonCount + item.Info.studentCount * 1
        } else if (item.Info.count) {
          allpersonCount = allpersonCount + item.Info.count * 1
        }
      })
    }
    if (data?.departments_pop?.length) {
      data.departments_pop.map(item => {
        if (item.Info.totalCount) {
          allpersonCount = allpersonCount + item.Info.totalCount * 1
        }
      })
    }
    if (data?.persons_pop?.length) {
      allpersonCount = allpersonCount + data.persons_pop.length * 1
    }

    if (data?.grades_pop?.length) {
      data.grades_pop.map(item => {
        if (item.Info.totalCount) {
          allpersonCount = allpersonCount + item.Info.totalCount * 1
        }
      })
    }

    if (data?.classes_pop?.length) {
      data.classes_pop.map(item => {
        if (item.Info.totalCount) {
          allpersonCount = allpersonCount + item.Info.totalCount * 1
        }
        if(item.Info.count){
          allpersonCount = allpersonCount + item.Info.count * 1
        }
      })
    }

    console.log(allpersonCount, data)
    return allpersonCount
  }

  getChooseMainList(datalist) {
    let noticeNature = this.state.noticeNature
    if (noticeNature != undefined && getInfo.getSchoolType() != 1) {
      if (noticeNature == 2) {//年级《2》
        return []
      } else if (noticeNature == 3) {//班级
        return []
      }
    }

    if (!datalist) {
      return []
    }
    let chooseMainList: number[] = []
    datalist.map(item => {
      if (item.type[0] == 0) {
        if (chooseMainList.findIndex(item2 => item2 == 1) == -1) {
          chooseMainList.push(1)
        }
      } else if (item.type[0] == 1 || item.type[0] == 7) {
        if (chooseMainList.findIndex(item2 => item2 == 2) == -1) {
          chooseMainList.push(2)
        }
      } else if (item.type[0] == 2 || item.type[0] == 5) {
        if (chooseMainList.findIndex(item2 => item2 == 3) == -1) {
          chooseMainList.push(3)
        }
      } else if (item.type[0] == 3 || item.type[0] == 6) {
        if (chooseMainList.findIndex(item2 => item2 == 4) == -1) {
          chooseMainList.push(4)
        }
      }
    })
    return chooseMainList
  }
  //===================================

  /**
 * 对象数组中是否有和该对象键值相同的项
 * @param arr 对象数组
 * @param item 对象
 * @param key 键值
 * @returns boolean true代表对象数组包含这个对象
 */
  useArraySome = (arr, item, key: string) => {
    if (!Array.isArray(arr)) {
      return false
    }

    return arr.some(child => child[key] === item[key])
  }

  initChoLibrary = (data, userList) => {
    if ((!Array.isArray(data) || !data.length) || (!Array.isArray(userList) || !userList.length)) {
      return
    }

    data.forEach((item: any) => {
      if (item.Info && item.Info?.userId) {
        if (this.useArraySome(userList, item.Info.userId, 'userId')) {
          item.hasCho = 1
        }
      } else if (item.subData) {
        this.initChoLibrary(item.subData, userList)
      }
    })
  }

  countItemsWithProperty = (dataList, count = 0) => {



    if (Array.isArray(dataList)) {
      dataList.forEach(item => {
        if (item.Info?.userId && item.hasCho == 1) {
          count++
        } else if (item.subData) {
          this.countItemsWithProperty(item.subData, count)
        }
      });
    }

    return count;
  }


  render() {
    const itemCount = this.state.receivers?.persons?.length
    let dataList = this.state.choLibrary
    let chooseMainList = this.getChooseMainList(dataList)
    let AllpersonCount = this.getAllpersonCount(this.state.receiver_pop) ||itemCount
    let env = getInfo.getEnv()

    this.initChoLibrary(dataList, this.state.receivers?.persons)


    return (
      <View className='chooseObject'>
        {
          env === "weapp" ? "" :
            <LgNavCustomBar
              mainTitle={'选择对象'}
              needMainIcon={process.env.TARO_ENV === 'h5' ? true : false}
              needBackIcon={true}
              isTitlePositionLeft={true}
              mainTitleBold={false}
              onClick={this.goBack}
            ></LgNavCustomBar>
        }
        <View className='search'>
          <LgSearch onConfirm={this.onSearch} onInput={this.onInput} placeholder={"搜索姓名/学号/工号..."} />
          <View className='reflash' onClick={this.reflash}>刷新</View>
        </View>

        <View className='scroll'>
          {
            chooseMainList.length == 0 || (this.state.isCollegeAdmin && this.state.noticeNature==1) ? "" :
              <View className='top_list'>
                {
                  chooseMainList?.map((item, index) => {
                    if (item == 1) {
                      return (
                        <View key={"chooseMainList_" + index} className='item' onClick={() => this.chooseMainObject(1)}>
                          <View className='img img_adm'></View>
                          <View className='text'>全体管理员</View>
                        </View>
                      )
                    } else if (item == 2) {
                      return (
                        <View key={"chooseMainList_" + index} className='item' onClick={() => this.chooseMainObject(2)}>
                          <View className='img img_teachers'></View>
                          <View className='text'>全体教师</View>
                        </View>
                      )
                    } else if (item == 3) {
                      return (
                        <View key={"chooseMainList_" + index} className='item' onClick={() => this.chooseMainObject(3)}>
                          <View className='img img_students'></View>
                          <View className='text'>全体学生</View>
                        </View>
                      )
                    } else if (item == 4) {
                      return (
                        <View key={"chooseMainList_" + index} className='item' onClick={() => this.chooseMainObject(4)}>
                          <View className='img img_parents'></View>
                          <View className='text'>全体家长</View>
                        </View>
                      )
                    }
                  })
                }
              </View>
          }


          <View className='choose_list'>
            {
              dataList ?
                <LgSeleTarget
                  type="B"
                  setVal={(fn) => { this.setVal = fn }}
                  getVal={(fn) => { this.getVal = fn }}
                  // getSubList={this.getSubList.bind(this)}
                  choNumUnit="个"
                  // dataList={this.handleGetDataList("", 1, 0)} 
                  dataList={dataList}
                  subListAttr={["subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData", "subData"]}
                  showNameAttr={["name", "name", "name", "name", "name", "name", "name", "name", "name", "name", "name", "name", "name", "name", "name", "name", "name", "name", "name", "name"]}
                  // onConfirm={this.onConfirm}//点击确定按钮的回调
                  confirmBtnName={"点击确定"}//确定按钮显示名字
                  // getNumber={()=>"全校师生"}
                  getSubList={this.getNewData}//获取子项数据
                  onChangeData={this.onChangeData}//数据改变时调用
                // chooseItem = {(fn) => {this.chooseItem = fn}}
                />
                : ""
            }
            <View className='search_list' style={{ zIndex: this.state.searchOn ? 10 : -10 }}>
              <View className='search_list_nav'>
                <View className='search_list_nav_text'>搜索结果</View>
                <View className='search_list_nav_close' onClick={this.closeSearchList}>关闭</View>
              </View>
              {
                this.state.searchLoading 
                
                ?
                  <View className="search_loading">
                    <PrLoading
                      isOpened={true}
                      content={"搜索中"}
                      mode="center"
                    ></PrLoading>
                  </View>
                  :
                  <>
                    {
                      this.state.searchArr.map((item, index) => {
                        return (
                          <View className='search_list_item' key={index}>
                            <LgCheckbox type='B' value={item.hasCho} onClick={(value) => this.searchListOnClick(value, index, item)}>{item.name}</LgCheckbox>
                          </View>
                        )
                      })
                    }
                    {
                      this.state.searchArr.length == 0 ?
                        <View className='no_contant'>
                          <LgEmptyContent
                            text={"内容为空"}
                            type="B"
                          ></LgEmptyContent>
                        </View>
                        : ""
                    }
                  </>
              }


            </View>
          </View>

        </View>

        <View className='chooseObject_btnBox'>
          <View className='text' onClick={this.openPop}>已选择{AllpersonCount}人</View>
          <LgButton className='btn_chooseObject' type='B' onClick={() => this.onConfirm(1)}>确定</LgButton>
        </View>

        <PrPopChooseObject
          isOpen={this.state.isPopOpen}
          data={this.state.receiver_pop}
          receivers={this.state.receivers}
          onClosePop={this.closePop}//关闭弹窗
          deleteItem={this.deleteChoItem}//移除所选
          deleteItem2={this.deleteChoItem2}//移除所选(接口返回的对象)
        ></PrPopChooseObject>

      </View>
    )
  }
}
